package org.wesc.boot.webapi.api.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.wesc.boot.common.annotation.ApiEndpoint;
import org.wesc.boot.common.api.R;
import org.wesc.boot.dao.domain.QueryRequest;
import org.wesc.boot.dao.entity.User;
import org.wesc.boot.dao.entity.UserMessage;
import org.wesc.boot.dao.model.UserMessageModel;
import org.wesc.boot.secure.jwt.JwtUtil;
import org.wesc.boot.service.system.notice.UserMessageAdvanceService;
import org.wesc.boot.service.system.user.UserAdvanceService;
import org.wesc.boot.webapi.base.BaseApi;

import java.util.Map;

/**
 * 用户消息接口
 * @author Wesley
 * @date 2021/4/1
 */
@Api(tags={"用户消息接口"})
@Slf4j
@Validated
@RestController
@RequestMapping("/api/userMessage")
public class UserMessageApi extends BaseApi {

    private final UserAdvanceService userAdvanceService;

    private final UserMessageAdvanceService userMessageAdvanceService;

    @Autowired
    public UserMessageApi(UserAdvanceService userAdvanceService,
            UserMessageAdvanceService userMessageAdvanceService) {
        this.userAdvanceService = userAdvanceService;
        this.userMessageAdvanceService = userMessageAdvanceService;
    }

    @ApiOperation(value = "查看我的消息")
    @GetMapping(value = "/list")
    public R<Map<String, Object>> getMyMessageList(QueryRequest request, UserMessageModel userMessageModel) {
        String currentUserName = JwtUtil.getCurrentUserName();
        User currentUser = this.userAdvanceService.findByUserName(currentUserName);
        userMessageModel.setUserId(String.valueOf(currentUser.getUserId()));
        return R.data(getDataTable(this.userMessageAdvanceService.pageQueryList(request, userMessageModel)));
    }

    @ApiOperation(value = "更新用户消息阅读状态")
    @PutMapping(value = "/updateReadFlag")
    public R<String> updateMessageReadFlag(String noticeId) {
        String currentUserName = JwtUtil.getCurrentUserName();
        User currentUser = this.userAdvanceService.findByUserName(currentUserName);
        String userId = String.valueOf(currentUser.getUserId());
        this.userMessageAdvanceService.updateMessageReadFlag(noticeId, userId);
        return R.success("成功更新阅读状态");
    }

    @ApiOperation(value = "一键已读")
    @PutMapping(value = "/readAll")
    @ApiEndpoint(operation = "一键已读", exception = "一键已读失败")
    public R<String> readAll() {
        String currentUserName = JwtUtil.getCurrentUserName();
        User currentUser = this.userAdvanceService.findByUserName(currentUserName);
        String userId = String.valueOf(currentUser.getUserId());
        this.userMessageAdvanceService.readAll(userId);
        return R.success("全部已读");
    }

    @ApiOperation(value = "通过id查询消息")
    @GetMapping(value = "/queryById")
    public R<UserMessage> queryById(@RequestParam(name="id") String id) {
        UserMessage userMessage = this.userMessageAdvanceService.queryById(id);
        if(null == userMessage) {
            return R.fail("未找到对应实体");
        }else {
            return R.data(userMessage);
        }
    }
}
